package cn.yj.core.business.controller;

import cn.yj.annotation.pagehelper.page.OrderBy;
import cn.yj.annotation.pagehelper.page.PageUtils;
import cn.yj.cgs.dba.entity.IntegralGoods;
import cn.yj.cgs.dba.entity.vo.IntegralGoodBuyVo;
import cn.yj.cgs.dba.entity.vo.IntegralGoodVo;
import cn.yj.syslog.common.LogType;
import cn.yj.syslog.common.OperateLog;
import cn.yj.config.annotation.Anonymous;
import cn.yj.config.web.AppBaseController;
import cn.yj.core.business.service.IntegralGoodsService;
import cn.yj.entity.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author 永健
 * @since 2021-05-07 14:41
 */
@RestController
@RequestMapping("/integral/goods")
public class IntegralGoodsController extends AppBaseController<IntegralGoods> {

    @Resource
    private IntegralGoodsService goodsService;

    @Anonymous
    @GetMapping("/getList")
    public R getList(@RequestParam Map<String, Object> params) {
        return success(goodsService.pageList(params,
                PageUtils.page(new OrderBy(OrderBy.Direction.DESC, "create_time"))));
    }

    @PostMapping("/addGoods")
    public R addGoods(@RequestBody @Valid IntegralGoodVo integralGoodVo) {
        return result(goodsService.addGoods(integralGoodVo));
    }

    @GetMapping("/getGoodsType")
    @Anonymous
    public R getGoodsType() {
        return success(goodsService.getGoodsType());
    }


    @PostMapping("/updateGoods")
    public R updateGoods(@RequestBody @Valid IntegralGoodVo integralGoodVo) {
        return result(goodsService.updateGoodsByCode(integralGoodVo));
    }

    @OperateLog(describe = "兑换商品",type = LogType.UPDATE)
    @PostMapping("/toBuy")
    public R progress(@RequestBody IntegralGoodBuyVo buyVo) {
        buyVo.setUserCode(getUserCode());
        return goodsService.goBuy(buyVo)?success("兑换成功，可到积分中心查看记录"):error("兑换失败");
    }

    @GetMapping("/detail/{goodsNo}")
    public R detail(@PathVariable String goodsNo) {
        return success(goodsService.getDetail(goodsNo));
    }

}
